<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['diy']=$this->plugin_setting('diy');
if($_RQ['op']=='main'){
	$setting=$this->plugin_setting('basic');
	$setting['bankuai1']=json_decode($setting['bankuai1'],true);
	$setting['bankuai2']=json_decode($setting['bankuai2'],true);
	$setting['footernav']=json_decode($setting['footernav'],true);
	$setting['plugins']=json_decode($setting['plugins'],true);
	$plugins = $this->plugins();
	$bankuai1=pdo_getall('jy_gohome_serve_category',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
	//print_r($bankuai1);exit;
	$return['bankuai1']=$bankuai1;
	$setting['bankuai2'] =  !empty($setting['bankuai2']) ? $setting['bankuai2'] : $plugins[1]['list'];
	foreach($setting['bankuai2'] as $item){
		if($item['is_install'] == 0){
			unset($item);
		}
	}
	$footernav = array(
			array('title'=>'首页','icon'=>'icon-shouye','color'=>'#cc99ff','action'=>'jy_cms/index/','image'=>'../addons/jy_cms/resource/images/jy_cmsarticle.png'),
			array('title'=>'附件','icon'=>'icon-map','color'=>'#66cc99','action'=>'jy_cms/nearlist/','image'=>'../addons/jy_cms/resource/images/jy_fenleimsg.png'),
			array('title'=>'发现','icon'=>'icon-browse','color'=>'#ff9933','action'=>'jy_cms/foundlist/','image'=>'../addons/jy_cms/resource/images/jy_fenleimsg.png'),
			array('title'=>'订单','icon'=>'icon-manageorder','color'=>'#66cc66','action'=>'jy_cms/order/','image'=>'../addons/jy_cms/resource/images/jy_fenleimsg.png'),
			array('title'=>'我的','icon'=>'icon-wo1','color'=>'#FF83FA','action'=>'jy_cms/myhome/','image'=>'../addons/jy_cms/resource/images/jy_fenleimsg.png'),
	);
	$setting['footernav'] = !empty($setting['footernav']) ? $setting['footernav'] : $footernav;
	$return['list']=array();
	$return['province']=file_get_contents('core/resource/area.txt');
	$return['province']=unserialize($return['province']);
	$return['pcd']=array(
		'province' =>trim($_RQ['province']),
		'city' =>trim($_RQ['city']),
		'district' =>trim($_RQ['district']),
	);
	$jy_gohome=pdo_get('core_plugins',array('name'=>'jy_gohome'));
	if(!empty($jy_gohome)){
		$worker_show=pdo_getall('jy_gohome_worker',array('uniacid'=>$_SESSION['uniacid'],'isshow'=>'1','status'=>'1','islock'=>'0'));
		if($worker_show){
			foreach($worker_show as &$item){
				$num1=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'worker_id'=>$item['id'],'status'=>'1'));
				$num2=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'worker_id'=>$item['id'],'status'=>'2'));
				$num3=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'worker_id'=>$item['id'],'status'=>'4'));
				$item['num']=$num1+$num2+$num3;
			}
		}
		$serve_show=pdo_getall('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'isshow'=>'1','status'=>'1'),'','','paixu desc');
		if($serve_show){
			foreach($serve_show as &$item){
				$num=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$item['id'],'status'=>array(1,2,3)));
				$item['num']=$item['num']+$num;
				$item['allstar']=pdo_getsum('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$item['id']),'star');
				$item['isping']=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$item['id'],'star >'=>0));
				if($item['isping']){
					$item['star']=$item['allstar']/$item['isping'];
				}else{
					$item['star']=5;
				}
			}
		}
		$return['worker_show']=$worker_show;
		$return['serve_show']=$serve_show;
	}
	
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$pindex = max(1, intval($post['page']));
	$psize = 8;
	$total='';
	$jy_jd=pdo_get('core_plugins',array('name'=>'jy_jd'));
	if(!empty($jy_jd)){
		$return['jdlist']=pdo_getslice('jy_jd_hotel',$params,array($pindex,$psize),$total,$fields,'','id desc');
		foreach($return['jdlist'] as &$v){
			$price=pdo_getall('jy_jd_room',array('hotelid'=>$v['id']));
			
			$price=json_decode($price['0']['taocan'],true);
			
			if(is_array($price)){
				paixu($price,'price');
			}
			$v['price']=$price['0']['price'];
			$v['lat_lng']=json_decode($v['lat_lng'],true);
			$v['juli']=cfc('tools')->getdistance($post['lng'],$post['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
			$v['juli']=intval($v['juli']/1000);
			$id=$v['id'];
			$_SESSION['juli'][$id]=$v['juli'];
		}
	}
	$return['setting']=$setting;
	$return['params']=array();
	$htmltitle = $setting['title'];
	$share_desc = $setting['share_desc'];
	$share_image = $setting['share_image'];
	$return['sharedata']=array(
			'title'=>$htmltitle?:'上门服务',
			'desc'=>$share_desc?:'上门服务，技师入驻，经络排毒，活血化瘀！',
			'link'=>custom_url('jy_gohome/index//&fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true),
			'imgUrl'=>ATTACHMENT_URL.$share_image,
	);
	set_html('title',$setting['title']?$setting['title'] :'上门服务');
	exi($return);
}
if($_RQ['op']=='getlist'){
	$post=get_postdata();
	$post['page']=$post['page']?:$_RQ['page'];
	$pindex = max(1, intval($post['page']));
	$psize = 16;
	$total='';
	$params1=array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','isshow'=>1);
	$params2=array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','isshow'=>1,'islock'=>0);
	if(trim($_RQ['keyword'])){
		$params1['title like']='%'.trim($_RQ['keyword']).'%';
		$params2['name like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	$post['province']=$post['province']?:trim($_RQ['province']);
	$post['city']=$post['city']?:trim($_RQ['city']);
	if($post['province']){
		if($post['city']){
			//查询商户id
			$merch_id=pdo_getcolumn('jy_gohome_merch',array('uniacid'=>$_SESSION['uniacid'],'city'=>$post['city']),'id');
			if($merch_id>0){
				//$params1['merchid']=$merch_id;
				//$params2['merchid']=$merch_id;
			}
		}else{
			$merch_id=pdo_getcolumn('jy_gohome_merch',array('uniacid'=>$_SESSION['uniacid'],'province'=>$post['province']),'id');
			if($merch_id>0){
				//$params1['merchid']=$merch_id;
				//$params2['merchid']=$merch_id;
			}

		}
	}
	$worker_show=pdo_getslice('jy_gohome_worker',$params2,array($pindex,$psize),$total,$fields,'','id desc');
	$serve_show=pdo_getslice('jy_gohome_serve',$params1,array($pindex,$psize),$total,$fields,'','paixu desc, id desc');
	foreach($serve_show as &$item){
		$item['allstar']=pdo_getsum('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$item['id']));
		$item['isping']=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$item['id'],'star >'=>0));
		$item['star']=$item['allstar']/$item['isping'];
	}
	$return['serve_show']=$serve_show;
	$return['list']=$return['worker_show']=$worker_show;
	exi($return);
}
if($_RQ['op']=='getworkerlist'){
	$setting=$this->plugin_setting('basic');
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
	}
	$pindex = max(1, intval($post['page']));
	$psize = 1000;
	$total='';
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','isshow'=>1,'islock'=>0);
	if(trim($_RQ['keyword'])){
		$params['name like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	if(isset($_RQ['status'])){
		if(intval($_RQ['status'])==2){
			$params['islock']=1;
		}elseif(intval($_RQ['status'])==3){
			$params['status']=0;
		}
		$return['params']['status']=trim($_RQ['status']);
	}
	$list=pdo_getslice('jy_gohome_worker',$params,array($pindex,$psize),$total,$fields,'','looks desc');
	foreach($list as $k=>&$v){
		$v['headimg']=tomedia($v['headimg']);
		$v['lat_lng']=$v['lat_lng']?:'{}';
		$v['lat_lng']=json_decode($v['lat_lng'],true);
		$v['tags']=$v['tags']?:$setting['tags'];
		$v['tags']=explode('|',$v['tags']);
		$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
		$v['juli']=intval($v['juli']/1000);
		$v['sales']=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'status >'=>0,'worker_id'=>$v['id']),'id');
		$v['starall']=pdo_getsum('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>4,'worker_id'=>$v['id']),'star');
		$v['starcount']=pdo_count('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>4,'worker_id'=>$v['id'],'star >'=>0),'id');
		if($v['starcount'] && $v['starall']){
			$v['stars']=$v['starall']/$v['starcount'];
		}
		$v['stars']=$v['stars']?:5;
		if($v['islock']==0 && $v['status']==1){
			$v['statustitle']='可接单';
		}elseif($v['islock']==1 && $v['status']==1){
			$v['statustitle']='服务中';
		}else{
			$v['statustitle']='休息中';
		}
		
	}
	if(trim($_RQ['order'])){
		$return['params']['order']=trim($_RQ['order']);
		if(trim($_RQ['order'])=='default'){
			paixu($list,'juli');
		}elseif(trim($_RQ['order'])=='star'){
			paixu($list,'stars');
		}elseif(trim($_RQ['order'])=='sales'){
			paixu($list,'sales');
		}elseif(trim($_RQ['order'])=='looks'){
			paixu($list,'looks');
		}
	}else{
		paixu($list,'juli');
	}
	if($_RQ['limit']){
		$list=array_slice($list,0,intval($_RQ['limit']));
	}else{
		$page=intval($_RQ['page'])?:1;
		$start=($page-1)*10;
		$list=array_slice($list,$start,10);
	}
	$return['list']=$list;
	exi($return);
}	
?>